// JScript source code

window.onload = onloadDocument;

function onloadDocument() 
{
  // create the controlbar on the top
  new ControlBar("controlBar");      
}
    
function showAdvancedOptions()
{
  var element = document.getElementById("chartAdvancedOptions");
  var disclose = document.getElementById("disclose");
  if(element.style.display == "none")
  {
    element.style.display = "";
    disclose.src = "./images/minus.png";
  }
  else
  {
    element.style.display = "none";
    disclose.src = "./images/plus.png";
  }
}

function createCallback(owner, func)
{
  // create a function that sets up "this" and delegates all of the parameters
  // to the passed in function
  var proxyFunction = new Function(
    "var f=arguments.callee; return f._func.apply(f._owner, arguments);");

  // attach owner to the created function
  proxyFunction._owner = owner;

  // attach function to delegate to
  proxyFunction._func = func;

  return proxyFunction;
}

function ControlBar(parentId)
{  
  var rootElem = this._rootElement = document.getElementById(parentId);
  
  var row = rootElem.insertRow(-1);
  var cell = row.insertCell(-1);
  
  var scrollElem = document.createElement("a");
  scrollElem.hRef = "";
  scrollElem.onclick = createCallback(this, this._scrollLeft);
  scrollElem.className = "controlBarLeftScroller_dis";
  scrollElem.innerHTML = "&nbsp;";
  this._scrollLeftElem = scrollElem;
  cell.appendChild(scrollElem);
  cell.align = "left";
    
  cell = row.insertCell(-1);
  var cellStyle = cell.style;
  cellStyle.height  = cellStyle.width = "100%";
  
  var itemContainer = this._itemContainer = document.createElement("div");
  itemContainer.className =  "controlBarItemContainer";
  cell.appendChild(itemContainer);
  
  var itemOptions = document.createElement("div");
  itemOptions.className = "controlBarItemOptions";
  itemContainer.appendChild(itemOptions);
  
  var changeSkin = document.createElement("a");
  changeSkin.innerHTML = "Change Skin";
  changeSkin.href = "#";
  changeSkin.id = "changeSkin";
  changeSkin.onclick = createCallback(this, this._changeSkin);
  changeSkin.className = "controlBarItemOptionsLink";
  itemOptions.appendChild(changeSkin);
  
  var toggle3D = document.createElement("a");
  toggle3D.innerHTML = "2D";
  toggle3D.href = "#";
  toggle3D.id = "changePerspective";
  toggle3D.onclick = createCallback(this, this._toggle3D);
  toggle3D.className = "controlBarItemOptionsLink";
  itemOptions.appendChild(toggle3D);

  var itemTableContainer = document.createElement("div");
  itemTableContainer.className =  "controlBarItemTableContainer";
  itemContainer.appendChild(itemTableContainer);
    
  var itemTable = this._itemTable = document.createElement("table");
  this._addItems(itemTable, CONTROL_BAR_ITEMS);
  itemTableContainer.appendChild(itemTable);
  
  cell = row.insertCell(-1);  
  scrollElem = document.createElement("a");
  scrollElem.hRef = "";
  scrollElem.onclick = createCallback(this, this._scrollRight);
  scrollElem.className = "controlBarRightScroller";
  scrollElem.innerHTML = "&nbsp;";  
  this._scrollRightElem = scrollElem;
  cell.appendChild(scrollElem);
  cell.align = "right";
}

// 30fps animation
ControlBar._ANIMATE_INTERVAL = 33;

// 1.5 s animation
ControlBar._ANIMATE_DURATION = 1500;

// The original item width, we will scale it 2 times on mouse over
ControlBar._ITEM_WIDTH = 90;
ControlBar._ITEM_HEIGHT = 47;
  
ControlBar.prototype._addItems = function(itemTable, barItems)
{
  var mouseOver = createCallback(this, this._mouseOverItem);
  var mouseOut = createCallback(this, this._mouseOutItem);
  
  var tableStyle = itemTable.style;
  tableStyle.position = "absolute";
  tableStyle.left = "0px";
  tableStyle.height = "100%";
  tableStyle.tableLayout = "fixed";
  tableStyle.zIndex = 100;
  
  itemTable.cellPadding = itemTable.cellSpacing = "0";
    
  var row = itemTable.insertRow(-1);
  var itemCount = barItems.length;
  var itemWidth = ControlBar._ITEM_WIDTH +"px";
  for(var i = 0; i < itemCount; ++i)
  {
    var itemInfo = barItems[i];
    var cell = row.insertCell(-1);
    cell.className = "controlBarItemCell";
    
    var anchor = document.createElement("a");
    anchor.id = itemInfo.id;
    anchor.className = "controlBarItem";
    cell.appendChild(anchor);

    var span = document.createElement("span");
    span.className = "controlBarItemText";
    span.appendChild(document.createTextNode(itemInfo.label));
    anchor.appendChild(span);
        
    img = document.createElement("img");
    img.className = "controlBarItemImage";
    img.src = "./images/"+itemInfo.image;
    // Set the initial height on the image so that in IE it looks right initially
    img.style.height = ControlBar._ITEM_HEIGHT+"px";
    anchor.appendChild(img);
    cell.onmouseover = mouseOver;
    cell.onmouseout = mouseOut;
    cell.style.width = anchor.style.width = itemWidth;
  }
}

ControlBar.prototype._scrollLeft = function()
{
  // If we are already animating
  if(this._intervalId)
    return;
  
  this._scrollRightElem.className = "controlBarRightScroller";
  
  var itemTable = this._itemTable;  
  if(itemTable.offsetLeft >= 0)
    return;
  
  if(this._animCallback == null)
     this._animCallback = createCallback(this, this.DoScroll);

  this._initialLeft = itemTable.offsetLeft;
  
  this._startTime = (new Date()).getTime();
  this._moveLeft = true;
  this._intervalId = window.setInterval(this._animCallback, ControlBar._ANIMATE_INTERVAL);
}

ControlBar.prototype._scrollRight = function()
{
  // If we are already animating
  if(this._intervalId)
    return;
  
  this._scrollLeftElem.className = "controlBarLeftScroller";
    
  var itemTable = this._itemTable;
  var tableWidth = itemTable.offsetWidth;
  var parentClientWidth = itemTable.parentNode.clientWidth;
  
  if(itemTable.offsetLeft <= -tableWidth + parentClientWidth)
    return;
  
  if(this._animCallback == null)
     this._animCallback = createCallback(this, this.DoScroll);

  this._initialLeft = itemTable.offsetLeft;

  this._startTime = (new Date()).getTime();
  this._moveLeft = false;
  this._intervalId = window.setInterval(this._animCallback, ControlBar._ANIMATE_INTERVAL);
}

ControlBar.prototype.DoScroll = function()
{
  var animateDuration = ControlBar._ANIMATE_DURATION;
  var diffTime = (new Date()).getTime() -  this._startTime;
  var ratio;
  
  if(diffTime >= animateDuration)
  {
    window.clearInterval(this._intervalId);
    delete this._intervalId;
    ratio = 1.0;
  }
  else
  {
    ratio = (diffTime)/animateDuration;
    
    // use slow fast slow animation
    ratio = (Math.cos(ratio*Math.PI + Math.PI) + 1) / 2;
  }
  
  var itemTable = this._itemTable;
  var tableWidth = itemTable.offsetWidth;
  var parentClientWidth = itemTable.parentNode.clientWidth;
  var totalDistance = parentClientWidth/2;
  
  var newLeft = this._initialLeft + 
                (this._moveLeft?totalDistance*ratio:-totalDistance*ratio);
  
  if(this._moveLeft && (newLeft > 0))
  {
    newLeft = 0;
    this._scrollLeftElem.className = "controlBarLeftScroller_dis";
  }
  else if(!this._moveLeft && (newLeft <= -tableWidth + parentClientWidth))
  {
    newLeft = -tableWidth + parentClientWidth;
    this._scrollRightElem.className = "controlBarRightScroller_dis";
  }
  itemTable.style.left = newLeft +"px";
}

ControlBar.prototype._mouseOverItem = function(e)
{
  if(!e)
    e = window.event;
    
  var anchorParent = e.target || e.srcElement;
  
  while(anchorParent.tagName != "TD")
  {
    anchorParent = anchorParent.parentNode;
  }
  anchor = anchorParent.firstChild;
  anchor.lastChild.style.height = 2*ControlBar._ITEM_HEIGHT+"px";
  anchor.firstChild.style.display = "block";
  anchor.style.width = anchorParent.style.width = 2*ControlBar._ITEM_WIDTH +"px";
}

ControlBar.prototype._mouseOutItem = function(e)
{
  if(!e)
    e = window.event;
    
  var anchorParent = e.target || e.srcElement;
  
  while(anchorParent.tagName != "TD")
  {
    anchorParent = anchorParent.parentNode;
  }
  var anchor = anchorParent.firstChild;
  anchor.lastChild.style.height = ControlBar._ITEM_HEIGHT+"px";
  anchor.firstChild.style.display = "none";
  anchor.style.width = anchorParent.style.width = ControlBar._ITEM_WIDTH +"px";
}

ControlBar.prototype._changeSkin = function()
{
  var skin = document.getElementById("skin");
  if(skin.value == "GradientLookAndFeel")
    skin.value = "TransparentLookAndFeel";
  else
    skin.value = "GradientLookAndFeel";
    
  this._reloadItemsTable(skin, null);

}

ControlBar.prototype._toggle3D = function()
{
  var perspective = document.getElementById("Perspective");
  perspective.checked = !perspective.checked;
  document.getElementById("changePerspective").innerHTML = perspective.checked?"2D":"3D";  
  
  this._reloadItemsTable(null, perspective);
}

ControlBar.prototype._reloadItemsTable = function(skin, perspective)
{
  if(!perspective)
    perspective = document.getElementById("Perspective");
  if(!skin)
    skin = document.getElementById("skin");
  
  this._scrollLeftElem.className = "controlBarLeftScroller_dis";
  var controlItems;  
  if(perspective.checked && skin.value == "GradientLookAndFeel")
    controlItems = CONTROL_BAR_ITEMS;
  else if(!perspective.checked && skin.value == "GradientLookAndFeel")
    controlItems = CONTROL_BAR_ITEMS_2D;
  else if(perspective.checked && skin.value == "TransparentLookAndFeel")
    controlItems = CONTROL_BAR_ITEMS_S2;
  else if(!perspective.checked && skin.value == "TransparentLookAndFeel")
    controlItems = CONTROL_BAR_ITEMS_2D_S2;
    
  this._itemTable.deleteRow(0);
  this._addItems(this._itemTable, controlItems);
}

function ControlBarItemInfo(label, id, image)
{
  this.label = label;
  this.id = id;
  this.image = image;
}

CONTROL_BAR_ITEMS = 
[
  new ControlBarItemInfo("Bar", "bar", "bar.png"),
  new ControlBarItemInfo("Pie", "pie", "pie.png"),
  new ControlBarItemInfo("Area", "area", "area.png"),
  new ControlBarItemInfo("Line", "line", "line.png"),
  new ControlBarItemInfo("Scatter", "scatter", "scatter.png"),
  new ControlBarItemInfo("Funnel", "funnel", "funnel.png"),
  new ControlBarItemInfo("Horizontal Bar", "hbar", "hbar.png"),
  new ControlBarItemInfo("Radar", "radar", "radar.png"),
  new ControlBarItemInfo("Radar Area", "radararea", "radararea.png"),
  new ControlBarItemInfo("Gauge", "gauge", "gauge.png"),
  new ControlBarItemInfo("SSemi-Circular", "semigauge", "semigauge.png"),
  new ControlBarItemInfo("Stacked bar", "stackedbar", "stackedbar.png"),
  new ControlBarItemInfo("Stacked Horizontal bar", "stackedhbar", "stackedhbar.png"),
  new ControlBarItemInfo("Stacked Area", "stackedarea", "stackedarea.png"),
  new ControlBarItemInfo("Cylinder bar", "cbar", "cbar.png"),
  new ControlBarItemInfo("Bar Line Combo", "barline", "barline.png"),
  new ControlBarItemInfo("Bar Area Combo", "bararea", "bararea.png")
];

CONTROL_BAR_ITEMS_S2 = 
[
  new ControlBarItemInfo("Bar", "bar", "bar2.png"),
  new ControlBarItemInfo("Pie", "pie", "pie2.png"),
  new ControlBarItemInfo("Area", "area", "area2.png"),
  new ControlBarItemInfo("Line", "line", "line2.png"),
  new ControlBarItemInfo("Scatter", "scatter", "scatter2.png"),
  new ControlBarItemInfo("Funnel", "funnel", "funnel2.png"),
  new ControlBarItemInfo("Horizontal Bar", "hbar", "hbar2.png"),
  new ControlBarItemInfo("Radar", "radar", "radar2.png"),
  new ControlBarItemInfo("Radar Area", "radararea", "radararea2.png"),
  new ControlBarItemInfo("Gauge", "gauge", "gauge2.png"),
  new ControlBarItemInfo("Semi-Circular Gauge", "semigauge", "semigauge2.png"),
  new ControlBarItemInfo("Stacked bar", "stackedbar", "stackedbar2.png"),
  new ControlBarItemInfo("Stacked Horizontal bar", "stackedhbar", "stackedhbar2.png"),
  new ControlBarItemInfo("Stacked Area", "stackedarea", "stackedarea2.png"),
  new ControlBarItemInfo("Cylinder bar", "cbar", "cbar2.png"),
  new ControlBarItemInfo("Bar Line Combo", "barline", "barline2.png"),
  new ControlBarItemInfo("Bar Area Combo", "bararea", "bararea2.png")
];

CONTROL_BAR_ITEMS_2D = 
[
  new ControlBarItemInfo("Bar", "bar", "bar2d.png"),
  new ControlBarItemInfo("Pie", "pie", "pie2d.png"),
  new ControlBarItemInfo("Area", "area", "area2d.png"),
  new ControlBarItemInfo("Line", "line", "line2d.png"),
  new ControlBarItemInfo("Scatter", "scatter", "scatter2d.png"),
  new ControlBarItemInfo("Funnel", "funnel", "funnel2d.png"),
  new ControlBarItemInfo("Horizontal Bar", "hbar", "hbar2d.png"),
  new ControlBarItemInfo("Radar", "radar", "radar.png"),
  new ControlBarItemInfo("Radar Area", "radararea", "radararea.png"),
  new ControlBarItemInfo("Gauge", "gauge", "gauge.png"),
  new ControlBarItemInfo("Semi-Circular Gauge", "semigauge", "semigauge.png"),
  new ControlBarItemInfo("Stacked bar", "stackedbar", "stackedbar2d.png"),
  new ControlBarItemInfo("Stacked Horizontal bar", "stackedhbar", "stackedhbar2d.png"),
  new ControlBarItemInfo("Stacked Area", "stackedarea", "stackedarea2d.png"),
  new ControlBarItemInfo("Bar Line Combo", "barline", "barline2d.png"),
  new ControlBarItemInfo("Bar Area Combo", "bararea", "bararea2d.png")
];

CONTROL_BAR_ITEMS_2D_S2 = 
[
  new ControlBarItemInfo("Bar", "bar", "bar2d2.png"),
  new ControlBarItemInfo("Pie", "pie", "pie2d2.png"),
  new ControlBarItemInfo("Area", "area", "area2d2.png"),
  new ControlBarItemInfo("Line", "line", "line2d2.png"),
  new ControlBarItemInfo("Scatter", "scatter", "scatter2d2.png"),
  new ControlBarItemInfo("Funnel", "funnel", "funnel2d2.png"),
  new ControlBarItemInfo("Horizontal Bar", "hbar", "hbar2d2.png"),
  new ControlBarItemInfo("Radar", "radar", "radar2.png"),
  new ControlBarItemInfo("Radar Area", "radararea", "radararea2.png"),
  new ControlBarItemInfo("Gauge", "gauge", "gauge2.png"),
  new ControlBarItemInfo("Semi-Circular Gauge", "semigauge", "semigauge2.png"),
  new ControlBarItemInfo("Stacked bar", "stackedbar", "stackedbar2d2.png"),
  new ControlBarItemInfo("Stacked Horizontal bar", "stackedhbar", "stackedhbar2d2.png"),
  new ControlBarItemInfo("Stacked Area", "stackedarea", "stackedarea2d2.png"),
  new ControlBarItemInfo("Bar Line Combo", "barline", "barline2d2.png"),
  new ControlBarItemInfo("Bar Area Combo", "bararea", "bararea2d2.png")
];